<template>
	<view class="">
		<u-popup :show="show" mode="right" width="100%" :zoom="false" @open="formzinit()">
			<u-navbar title="分润设置" @leftClick="
          () => {
            show = !show;
          }
        " :autoBack="false"></u-navbar>
			<formz ref="formz" :inputs="inputs"></formz>

			<fixedbtn>
				<view style="padding: 0 22rpx; flex: 1">
					<u-button :loading="loading" :custom-style="btnstyle" type="primary" @click="updata()">
						{{$z.$t("添加分润方")}}
					</u-button>
				</view>
			</fixedbtn>
		</u-popup>
	</view>
</template>

<script>
	import json from "./json.js";
	export default {
		data() {
			return {
				btnstyle: {
					flex: "1",
				},
				loading: false,
				show: false,
				inputs: json.Uaddjson,
			};
		},
		mounted() {},
		methods: {
			init(data) {
				this.inputs.forEach((e) => {
					if (e.value == "pcs") {
						e.defaultValue = data.pcs;
					}
				});

				this.show = true;
			},
			async formzinit() {
				this.$refs.formz.init();
			},
			async updata() {
				let data = await this.$refs.formz.validate();
				if (!data.udj) {
					data.udj = "0";
				}
				if (data.utype == "18" || data.utype == "19" || data.utype == "20") {
					data.pcs = this.$store.state.user.USearch.pcs;
				}
				await this.$z.request("/Uadd", data);
				let qbUSearch = await this.$z.request("/USearch", {
					uUser: data.uUser,
				});
				if (qbUSearch.length == "0") {
					this.$u.toast(this.$z.$t("添加失败"));
					return;
				}
				let dqUSearch = qbUSearch[0];
				let p = this.$u.$parent.call(this, "StatFrecharts");
				let fr = p.sybfb(
					p.StatFr.filter((e) => {
						return e.mode == p.mode;
					})
				);
				if (fr < 0) {
					fr = "0";
				}
				let adddata = {
					frid: "new" + p.StatFr.length,
					per: fr,
					pcs: p.query.id,
					pfrid: "0",
					pernx: "0",
					nxl: "0",
					mode: p.mode,
				};
				let uUsert_fridt = [{
						fridtype: "0",
						uUsert: ["0", "14", "18", "19"],
					},
					{
						fridtype: "2",
						uUsert: ["11", "20"],
					},
					{
						fridtype: "5",
						uUsert: ["12"],
					},
				];
				let datatype = uUsert_fridt.find(function(e) {
					return (
						e.uUsert.findIndex(function(f) {
							return f == dqUSearch.utype;
						}) >= 0
					);
				});

				datatype = datatype ? datatype.fridtype : "0";

				adddata.type = datatype;
				adddata.pfrid = dqUSearch.frid;
				let typepfrids = {
					0: this.$z.$t("代理商"),
					2: this.$z.$t("物业"),
					5: this.$z.$t("自定义"),
					8: this.$z.$t("一级分润方"),
					9: this.$z.$t("二级分润方"),
				};
				let mc = dqUSearch.uxm ?
					dqUSearch.uxm :
					p.Station.pcs + "-" + typepfrids[datatype];
				adddata.mc = mc;
				p.USearchs.push(dqUSearch);
				let statfr = this.$u.deepClone(p.StatFr);

				statfr.push(adddata);
				p.StatFr = statfr;
				this.show = false;
			},
		},
	};
</script>

<style></style>